<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
class General_Model extends Model 
{
    function General_Model()
    {
        parent::Model();
		umask(0);  //   set quyen cho tat ca cac folder theo nhu thu muc goc
    }
	
    function sendEmail2($emails, $subject, $templateName, $data = array(), $mailType = 'html', $from = null)
    {
        global $CI;
        if (! is_array($emails)) {
            $emails = array($emails);
        }
        $$configEmail['mailtype'] = $mailType;
	    $CI->load->library('email', $configEmail);
	    $CI->email->set_newline("\r\n");
	    $CI->email->initialize($configEmail);
        /**
         * Load email template from database
         */
        $result = $this->db->get_where('cvj_template_mail', array('name'=> $templateName, 'enabled'=> 1));
        $result = $result->result_array();
        if (empty($result)) {
            return false;
        }
        $content = $result[0];
        /**
         * Load variables
         */
        extract($data);
        ob_start();
        echo eval ('?>'.$content['content']);
        $content = ob_get_contents();
        @ob_end_clean();
        /**
         * Send mail
         */
        try {
            foreach ($emails as $address){
                $CI->email->clear();
                $CI->email->to($address);
                $from = 'test@bjornekaelderen.dk';
                $CI->email->from($from);
                $CI->email->subject($subject);
                $CI->email->message($content);
                $CI->email->send();
            }
        } catch (Exception $e) {
            //Do nothing
        }
    }

/**********************************  CATEGORY MANAGER  ***********************************/
	function get_all_category($parent_id)
	{
		$category_data = array();
		$sql ="SELECT * FROM category c LEFT JOIN category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = " . (int)$parent_id . " ORDER BY c.sort_order, cd.name ASC";
		$query = $this->db->query($sql);
		foreach ($query->result_array() as $result) 
		{
			$category_data[] = array(
				'category_id' => $result['category_id'],
				'name'        => $this->getPath($result['category_id']),
				'info'        => $result['info'],
				'sort_order'  => $result['sort_order'],
				'parent_id' => $result['parent_id'],
				'active'  => $result['active']
			);
		
			$category_data = array_merge($category_data, $this->get_all_category($result['category_id']));
		}	
		return $category_data;
	}
	
	function getPath($category_id) 
	{
		$sql = "SELECT name, parent_id FROM category c LEFT JOIN category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = " . (int)$category_id . "  ORDER BY c.sort_order, cd.name ASC";
		
		$query = $this->db->query($sql);
		foreach ($query->result_array() as $category_info) 
		{
		  if ($category_info['parent_id']) 
		  {
			  return "&nbsp;---&rsaquo; " . $category_info['name']; // Sports >> Football
		  } 
		  else 
		  {
			  return $category_info['name'];
		  }
		}
	}
	function add_events($arr)
	{
		$this->db->trans_start(); 
		$this->load->helper(array('form', 'url'));
		
		$config['upload_path'] = './upload/news_picture/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '500';
		$config['encrypt_name']	=TRUE;    // rename to random string
		
		$this->load->library('upload', $config);
			
		if (!empty($_FILES['userfile']['name']))
		{
			if ( $this->upload->do_upload())
			{	
					$datax=$this->upload->data();
					$this->db->set('image',$datax['file_name']);
			}
		}
		$data = array(
			'title' => $arr['title'],
            'content' => $arr['maincontent'],
			'event_date' =>strftime("%Y-%m-%d", strtotime($arr['eventdate']))
            );
		$this->db->insert('events', $data); 
		$this->db->trans_complete();
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}

	}
	function update_events($arr)
	{
		$this->db->trans_start(); 
		$this->load->helper(array('form', 'url'));
		
		$config['upload_path'] = './upload/news_picture/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '500';
		$config['encrypt_name']	=TRUE;    // rename to random string
		
		$this->load->library('upload', $config);
			
		if (!empty($_FILES['userfile']['name']))
		{

			if ( $this->upload->do_upload())
			{	
					$datax=$this->upload->data();
					$this->db->set('image',$datax['file_name']);
					
			}
		}
		$data = array(
			'title' => $arr['title'],
            'content' => $arr['maincontent'],
			'event_date' =>strftime("%Y-%m-%d", strtotime($arr['eventdate']))
            );

		$this->db->where('id',$arr['id']);
		$this->db->update('events', $data); 
		$this->db->trans_complete();
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
	function get_gallery_manager($totalrow='',$num='', $offset='')
	{
		if ($totalrow=="totalrow")
		{
			
			$sql ="SELECT count(*) as result FROM album";
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('album');
				$this->db->limit($num, $offset);
				$this->db->order_by("created_date", "desc");
			//$this->db->where("(video!='NULL' or image!='NULL')");
			$query = $this->db->get();
			return $query;
		}
	}

	function get_album($id,$totalrow='',$num='', $offset='')
	{
		if ($totalrow=="totalrow")
		{
			
			$sql ="SELECT count(*) as result FROM gallery where album_id=".$id;
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('gallery');
				$this->db->limit($num, $offset);
				$this->db->where('album_id',$id);
				//$this->db->order_by("created_date", "desc");
			//$this->db->where("(video!='NULL' or image!='NULL')");
			$query = $this->db->get();
			return $query;
		}
	}
	function add_gallery($arr)
	{
		$this->db->trans_start(); 
		$this->load->helper(array('form', 'url'));
		
		$config['upload_path'] = './upload/gallery/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '1000';
		$config['encrypt_name']	=TRUE;    // rename to random string
		
		$this->load->library('upload', $config);
			
		if (!empty($_FILES['userfile']['name']))
		{
			if ( $this->upload->do_upload())
			{	
					$datax=$this->upload->data();
					$this->db->set('thumbnail',$datax['file_name']);
			}
		}
		$this->db->set('created_date',strtotime($arr['eventdate']));
		$this->db->set('total_item',0);
		$this->db->insert('album'); 
		$this->db->trans_complete();
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
	function add_image($arr)
	{
		$this->db->trans_start(); 
		$this->load->helper(array('form', 'url'));
		
		$config['upload_path'] = './upload/gallery/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '2000';
		$config['encrypt_name']	=TRUE;    // rename to random string
		
		$this->load->library('upload', $config);
			
		if (!empty($_FILES['Filedata']['name']))
		{
			if ( $this->upload->do_upload("Filedata"))
			{	
					$datax=$this->upload->data();
					$this->db->set('image',$datax['file_name']);
					$this->db->set('album_id',$arr['id']);
					$this->db->insert('gallery'); 
					// UPDATE total images in ALBUM
					$sql ="update album set total_item=total_item+1 where id=".$arr['id'];
					$this->db->query($sql);
			}
		}
		// upload multiple images
		/*$n=count($_FILES['Filedata']['name']);
		if($n>0)
		{
			if($this->upload->do_multiple_upload($_FILES['Filedata']))//If it is a succesfull...
			{
			  
			  		$this->db->set('image',$this->upload->get_unique_file_names());
					$this->db->set('album_id',$arr['id']);
					$this->db->insert('gallery'); 
					// UPDATE total images in ALBUM
					$sql ="update album set total_item=total_item+1 where id=".$arr['id'];
					$this->db->query($sql);
			  //$full_images= ",".implode(",", $this->upload->get_unique_file_names()).",";
			}
			else
			{
				return -1;
			}
		}*/
		$this->db->trans_complete();
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}	
	function get_albumName($id)
	{
		$this->db->select('*');
		$this->db->from('album');
		$this->db->where("id", $id);
		$query = $this->db->get();		
		return $query;
	}
	function delete_gallery($id)
	{
		// begin TRANSACTION
		$this->db->trans_start(); 
		
		$this->db->delete('album', array('id' => $id)); 
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}	
	function delete_image($id, $album_id)
	{
		// begin TRANSACTION
		$this->db->trans_start(); 
		
		$this->db->delete('gallery', array('id' => $id)); 
		$sql ="update album set total_item=total_item-1 where id=".$album_id;
		$this->db->query($sql);
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}	
	function get_events($totalrow,$num, $offset)
	{
		if ($totalrow=="totalrow")
		{
			
			$sql ="SELECT count(*) as result FROM events ";
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
			$this->db->select("*");
			$this->db->from('events');
			$this->db->limit($num, $offset);
			$this->db->order_by('event_date','desc');
			$query = $this->db->get();
			return $query;
		}
	}
	
	function delete_events($id)
	{
	// begin TRANSACTION
		$this->db->trans_start(); 
		
		$this->db->delete('events', array('id' => $id)); 
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
	function get_event_detail($id)
	{
		$sql="SELECT DATE_FORMAT( event_date, '%m/%d/%Y' ) as event_date,id,title,content,image FROM events where id=".$id;
		$query = $this->db->query($sql);
		return $query->result_array();
	}
	function update_status_event($arr)
	{
		$this->db->trans_start();
			$this->db->set('status',$arr['status_option']);
			$this->db->where('order_id',$arr['update_id']);
			$this->db->update('orders'); 
		$this->db->trans_complete();
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}

// ===== SLIDESHOW -- GALLERY================================
	function get_all_slideshow($id='')
	{
		$this->db->select('*');
		$this->db->from('slideshow');
		if ($id!='')
		{	
			$this->db->where('slideshow_id',$id);
		}
		else
		{
			$this->db->where('language_code','en');
		}
		$this->db->order_by('sorting','desc');
		$query = $this->db->get();		
		return $query;
	}

	function banner_manipulate($data)
	{
		$this->db->trans_start(); 
		$config['upload_path'] = './upload_file/slideshow/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '1000'; // 1MB
		$this->load->library('upload', $config);
		
        if($_FILES['image1']['name'])
		{
			
				if ( $this->upload->do_upload('image1'))
				{	
						$datax1=$this->upload->data();
				}
				else
				{
					return -1;
				}
		}
		else
		{
			$datax1['file_name']=NULL;				
		}
		if($_FILES['image2']['name'])
		{
			
				if ( $this->upload->do_upload('image2'))
				{	
						$datax2=$this->upload->data();
				}
				else
				{
					return -1;
				}
		}
		else
		{
			$datax2['file_name']=NULL;				
		}
		
		$code=array(1=>'en',2=>'tr');
		
		if($data['currentid']!='') // update
		{
				$this->db->where('slideshow_id',$data['currentid']);
				$this->db->where('language_code',$code[1]);
				$this->db->set('title',$data['txtBannerName1']);
				$this->db->set('sorting',$data['sorting']);
				$this->db->set('content',$data['txtBannerDescription1']);
				if($datax1['file_name']!=NULL)
				{
					$this->db->set('image',$datax1['file_name']);
				}
				$this->db->update('slideshow'); 
				
				$this->db->where('slideshow_id',$data['currentid']);
				$this->db->where('language_code',$code[2]);
				$this->db->set('sorting',$data['sorting']);
				$this->db->set('title',$data['txtBannerName2']);
				$this->db->set('content',$data['txtBannerDescription2']);
				if($datax2['file_name']!=NULL)
				{
					$this->db->set('image',$datax2['file_name']);
				}
				$this->db->update('slideshow'); 
		}
		else	//add
		{
				$this->db->set('language_code',$code[1]);
				$this->db->set('sorting',$data['sorting']);
				$this->db->set('title',$data['txtBannerName1']);
				$this->db->set('content',$data['txtBannerDescription1']);
				$this->db->set('image',$datax1['file_name']);
				$this->db->insert('slideshow'); 
				$id=$this->db->insert_id();
				$this->db->set('slideshow_id',$id);
				$this->db->set('language_code',$code[2]);
				$this->db->set('sorting',$data['sorting']);
				$this->db->set('title',$data['txtBannerName2']);
				$this->db->set('content',$data['txtBannerDescription2']);
				$this->db->set('image',$datax2['file_name']);
				$this->db->insert('slideshow'); 
		}
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
	function get_information($alias)
	{
		$this->db->select('*');
		$this->db->from('information_description');
		$this->db->join('information', 'information.information_id = information_description.information_id');
		$this->db->where('keyword', $alias);
		$query = $this->db->get();		
		return $query;
	}
	function information_manipulate($data)
	{
		$this->db->trans_start(); 
		$config['upload_path'] = './upload_file/article/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '1024';
		$this->load->library('upload', $config);
		
        if($_FILES['image']['name'])
		{
			
				if ( $this->upload->do_upload('image'))
				{	
						$datax1=$this->upload->data();
				}
				else
				{
					//echo $this->upload->display_errors('<p>', '</p>');
					return -1;
				}
		}
		else
		{
			$datax1['file_name']=NULL;				
		}
		
		$code=array(1=>'en',2=>'tr');
		
		if($data['currentid']!='') // update
		{
				$id=$data['currentid']; 
				if($datax1['file_name']!=NULL)
				{
					$this->db->set('picture',$datax1['file_name']);
					$this->db->where('information_id',$id);
					$this->db->update('information'); 
				}
				
				
				$this->db->where('information_id',$id);
				$this->db->where('language_code',$code[1]);
				$this->db->set('title',$data['txtName1']);
				$this->db->set('intro',$data['intro1']);
				$this->db->set('content',$data['maincontent1']);
				$this->db->update('information_description'); 
				
				$this->db->where('information_id',$id);
				$this->db->where('language_code',$code[2]);
				$this->db->set('title',$data['txtName2']);
				$this->db->set('intro',$data['intro2']);
				$this->db->set('content',$data['maincontent2']);
				$this->db->update('information_description');  
		}
		else	//add
		{
				
				$this->db->set('thedate',time());
				$this->db->set('picture',$datax1['file_name']);
				$this->db->insert('article'); 
				
				$id=$this->db->insert_id();
				$this->db->set('article_id',$id);
				$this->db->set('language_code',$code[1]);
				$this->db->set('title',$data['txtName1']);
				$this->db->set('intro',$data['intro1']);
				$this->db->set('content',$data['maincontent1']);
				$this->db->insert('article_description'); 
				
				$this->db->set('article_id',$id);
				$this->db->set('language_code',$code[2]);
				$this->db->set('title',$data['txtName2']);
				$this->db->set('intro',$data['intro2']);
				$this->db->set('content',$data['maincontent2']);
				$this->db->insert('article_description'); 
		}
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}	
	}
//============DOWNLOAD============

	function get_subscribers_manager($totalrow='',$num='', $offset='')
	{
		if ($totalrow=="totalrows")
		{
			
			$sql ="SELECT count(*) as result from subscribers";
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('subscribers');
				$this->db->limit($num, $offset);
				$this->db->order_by("date", "desc");
			$query = $this->db->get();
			return $query;
		}
	}
	function get_allsubscribers()
	{

			$this->db->select("*");
			$this->db->from('subscribers');
			$this->db->order_by("date", "desc");
			$query = $this->db->get();
			return $query;

	}
// ==============================	
//============DOWNLOAD============

	function get_download_manager($totalrow='',$num='', $offset='')
	{
		if ($totalrow=="totalrows")
		{
			
			$sql ="SELECT count(*) as result from download";
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('download_description');
				$this->db->join('download', 'download.id = download_description.download_id');
				$this->db->where('language_code', 'en');
				$this->db->limit($num, $offset);
			$query = $this->db->get();
			return $query;
		}
	}
	function download_manipulate($data)
	{
		$this->db->trans_start(); 
		$config['upload_path'] = './upload_file/download/';
		$config['allowed_types'] = 'doc|docx|pdf|xls|xlsx|gif|jpg|jpeg|png';
		$config['max_size']	= '60000';
		$this->load->library('upload', $config);
		
        if($_FILES['image']['name'])
		{
			
				if ( $this->upload->do_upload('image'))
				{	
						$datax1=$this->upload->data();
				}
				else
				{
					//echo $this->upload->display_errors('<p>', '</p>');
					return -1;
				}
		}
		else
		{
			$datax1['file_name']=NULL;				
		}
		
		$code=array(1=>'en',2=>'tr');
		
		if($data['currentid']!='') // update
		{
				
				if($datax1['file_name']!=NULL)
				{
					$this->db->set('downloadfile',$datax1['file_name']);
				}
				$id=$data['currentid']; 
				
				
				$this->db->where('id',$id);
				$this->db->set('category_id',$data['category']);
				$this->db->set('itemid',$data['itemid']);
				$this->db->update('download'); 
				
				
				$this->db->where('download_id',$id);
				$this->db->where('language_code',$code[1]);
				$this->db->set('name',$data['txtName1']);
				$this->db->update('download_description'); 
				
				$this->db->where('download_id',$id);
				$this->db->where('language_code',$code[2]);
				$this->db->set('name',$data['txtName2']);
				$this->db->update('download_description'); 
		}
		else	//add
		{
				$this->db->set('category_id',$data['category']);
				$this->db->set('itemid',$data['itemid']);
				$this->db->set('downloadfile',$datax1['file_name']);
				$this->db->insert('download'); 
				
				$id=$this->db->insert_id();
				$this->db->set('download_id',$id);
				$this->db->set('language_code',$code[1]);
				$this->db->set('name',$data['txtName1']);
				$this->db->insert('download_description'); 
				
				$this->db->set('download_id',$id);
				$this->db->set('language_code',$code[2]);
				$this->db->set('name',$data['txtName2']);
				$this->db->insert('download_description'); 
		}
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
	function get_download_detail($id)
	{
		$this->db->select('*');
		$this->db->from('download_description');
		$this->db->join('download', 'download.id = download_description.download_id');
		$this->db->where('id', $id);
		$query = $this->db->get();		
		return $query;
	}
//========= NEWS ========================
	function get_newsmanager($totalrow='',$num='', $offset='')
	{
		if ($totalrow=="totalrows")
		{
			
			$sql ="SELECT count(*) as result from article";
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('article_description');
				$this->db->join('article', 'article.id = article_description.article_id');
				$this->db->where('language_code', 'en');
				$this->db->limit($num, $offset);
				$this->db->order_by('thedate','desc');
			$query = $this->db->get();
			return $query;
		}
	}
	function get_news_detail($id)
	{
		$this->db->select('*');
		$this->db->from('article_description');
		$this->db->join('article', 'article.id = article_description.article_id');
		$this->db->where('id', $id);
		$query = $this->db->get();		
		return $query;
	}
	function news_manipulate($data)
	{
		$this->db->trans_start(); 
		$config['upload_path'] = './upload_file/article/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '1024';
		$this->load->library('upload', $config);
		
        if($_FILES['image']['name'])
		{
			
				if ( $this->upload->do_upload('image'))
				{	
						$datax1=$this->upload->data();
				}
				else
				{
					//echo $this->upload->display_errors('<p>', '</p>');
					return -1;
				}
		}
		else
		{
			$datax1['file_name']=NULL;				
		}
		
		$code=array(1=>'en',2=>'tr');
		
		if($data['currentid']!='') // update
		{
				$id=$data['currentid']; 
				if($datax1['file_name']!=NULL)
				{
					$this->db->set('picture',$datax1['file_name']);
					$this->db->where('id',$id);
					$this->db->update('article'); 
				}
				
				
				$this->db->where('article_id',$id);
				$this->db->where('language_code',$code[1]);
				$this->db->set('title',$data['txtName1']);
				$this->db->set('intro',$data['intro1']);
				$this->db->set('content',$data['maincontent1']);
				$this->db->update('article_description'); 
				
				$this->db->where('article_id',$id);
				$this->db->where('language_code',$code[2]);
				$this->db->set('title',$data['txtName2']);
				$this->db->set('intro',$data['intro2']);
				$this->db->set('content',$data['maincontent2']);
				$this->db->update('article_description');  
		}
		else	//add
		{
				
				$this->db->set('thedate',time());
				$this->db->set('picture',$datax1['file_name']);
				$this->db->insert('article'); 
				
				$id=$this->db->insert_id();
				$this->db->set('article_id',$id);
				$this->db->set('language_code',$code[1]);
				$this->db->set('title',$data['txtName1']);
				$this->db->set('intro',$data['intro1']);
				$this->db->set('content',$data['maincontent1']);
				$this->db->insert('article_description'); 
				
				$this->db->set('article_id',$id);
				$this->db->set('language_code',$code[2]);
				$this->db->set('title',$data['txtName2']);
				$this->db->set('intro',$data['intro2']);
				$this->db->set('content',$data['maincontent2']);
				$this->db->insert('article_description'); 
		}
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}

//========= EVENTS MANAGER ========================
	function get_eventsmanager($totalrow='',$num='', $offset='')
	{
		if ($totalrow=="totalrows")
		{
			
			$sql ="SELECT count(*) as result from events";
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('events_description');
				$this->db->join('events', 'events.id = events_description.events_id');
				$this->db->where('language_code', 'en');
				$this->db->limit($num, $offset);
				$this->db->order_by('begin_date','desc');
			$query = $this->db->get();
			return $query;
		}
	}
	function get_events_detail($id)
	{
		$this->db->select('*');
		$this->db->from('events_description');
		$this->db->join('events', 'events.id = events_description.events_id');
		$this->db->where('id', $id);
		$query = $this->db->get();		
		return $query;
	}
	function get_events_gallery($id)
	{
		$this->db->select('*');
		$this->db->from('gallery');
		$this->db->where('reference_id', $id);
		$query = $this->db->get();		
		return $query;
	}
	function events_manipulate($data)
	{
		$this->db->trans_start(); 
		$config['upload_path'] = './upload_file/article/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '6000';
		$this->load->library('upload', $config);
		$code=array(1=>'en',2=>'tr');
		
		if($data['currentid']!='') // update
		{
				$id=$data['currentid']; 
						
				$this->db->where('id',$id);
				$this->db->set('begin_date',strtotime($data['txtStartDate']));
				$this->db->set('end_date',strtotime($data['txtEndingDate']));
				$this->db->update('events');
				
				$this->db->where('events_id',$id);
				$this->db->where('language_code',$code[1]);
				$this->db->set('title',$data['title1']);
				$this->db->set('name',$data['name1']);
				$this->db->set('address',$data['address1']);
				$this->db->set('content',$data['maincontent1']);
				$this->db->update('events_description'); 
				
				$this->db->where('events_id',$id);
				$this->db->where('language_code',$code[2]);
				$this->db->set('title',$data['title2']);
				$this->db->set('name',$data['name2']);
				$this->db->set('address',$data['address2']);
				$this->db->set('content',$data['maincontent2']);
				$this->db->update('events_description');  
				
				for($i=0;$i<5;$i++)
				{
					$datax1['file_name']='';
					if($_FILES['image'.$i]['name'])
					{
						
							if ( $this->upload->do_upload('image'.$i))
							{	
									$datax1=$this->upload->data();
									$this->db->where('reference_id',$id);
									$this->db->where('num',$i);
									$this->db->set('image',$datax1['file_name']);
									$this->db->update('gallery');
									
							}
							else
							{
								return -1;
							}
					}
				}
		}
		else	//add
		{
				
				$this->db->set('begin_date',strtotime($data['txtStartDate']));
				$this->db->set('end_date',strtotime($data['txtEndingDate']));
				$this->db->insert('events'); 
				
				$id=$this->db->insert_id();
				$this->db->set('events_id',$id);
				$this->db->set('language_code',$code[1]);
				$this->db->set('title',$data['title1']);
				$this->db->set('name',$data['name1']);
				$this->db->set('address',$data['address1']);
				$this->db->set('content',$data['maincontent1']);
				$this->db->insert('events_description'); 
				
				$this->db->set('events_id',$id);
				$this->db->set('language_code',$code[2]);
				$this->db->set('title',$data['title2']);
				$this->db->set('name',$data['name2']);
				$this->db->set('address',$data['address2']);
				$this->db->set('content',$data['maincontent2']);
				$this->db->insert('events_description'); 
				
				for($i=0;$i<5;$i++)
				{
					if($_FILES['image'.$i]['name'])
					{
						
							if ( $this->upload->do_upload('image'.$i))
							{	
									$datax1=$this->upload->data();
							}
							else
							{
								//echo $this->upload->display_errors('<p>', '</p>');
								return -1;
							}
					}
					else
					{
						$datax1['file_name']=NULL;				
					}
					$this->db->set('reference_id',$id);
					$this->db->set('num',$i);
					$this->db->set('type',1);
					$this->db->set('image',$datax1['file_name']);
					$this->db->insert('gallery');
				}

		}
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
//============ CATEGORY LIST=======
	function get_category($type=0)
	{
				$this->db->select("*");
				$this->db->from('category_description');
				$this->db->join('category', 'category.category_id = category_description.category_id');
				$this->db->where('language_code', 'en');
				$this->db->where('type',$type);
			$query = $this->db->get();
			return $query;
	}
///======== EDIT ARTICLE===============
    function get_article_detail1($id=0)
	{
		$sql="SELECT * from article_description where id=".(int)$id;
        
		$query = $this->db->query($sql);
		
        return $query->row();

	}
//========== UPDATE ARTICLE
function update_news($arr)
	{
		$this->db->trans_start(); 
		$this->load->helper(array('form', 'url'));
		
		$config['upload_path'] = './upload/image_article/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '1024';
		$config['encrypt_name']	=TRUE;    // rename to random string
		
		$this->load->library('upload', $config);
			
		if (!empty($_FILES['userfile']['name']))
		{

			if ( $this->upload->do_upload())
			{	
					$datax=$this->upload->data();
					$this->db->set('image',$datax['file_name']);
					//var_dump($datax);
					
			}
		}
        $date=strftime("%Y-%m-%d", time());
		$data = array(
			'title' => $arr['title'],
			'small_text' => $arr['short_desc'],
			'sorting' => $arr['sorting'],
            'content' => $arr['maincontent'],
            'created' => $date
            );

		$this->db->where('id',$arr['id']);
		$this->db->update('article_description', $data); 
		$this->db->trans_complete();
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}	
// ==============  RESERVATION MANAGER ======================	

	function get_reservations($totalrow,$_type='',$_keyword='',$num, $offset)
	{
		if ($totalrow=="totalrow")
		{
			
			$mytype='';
			$type='';
			if ($_type==0)
			{
				$type=" where ((email LIKE '%".$_keyword."%' ) OR (name LIKE '%".$_keyword."%' )  OR (date LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (info LIKE '%".$_keyword."%' ))";
			}
			if ($_type==1)
			{
				$type=" and ((email LIKE '%".$_keyword."%' ) OR (name LIKE '%".$_keyword."%' )  OR (date LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (info LIKE '%".$_keyword."%' ))";
				$mytype=" where status=0";
			}
			if ($_type==2)
			{
				$type=" and ((email LIKE '%".$_keyword."%' ) OR (name LIKE '%".$_keyword."%' )  OR (date LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (info LIKE '%".$_keyword."%' ))";
				$mytype=" where status=1";
			}
			if ($_type==3)
			{
				$type=" and ((email LIKE '%".$_keyword."%' ) OR (name LIKE '%".$_keyword."%' ) OR (date LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (info LIKE '%".$_keyword."%' ))";
				$mytype=" where status=2";
			}
			if($_keyword!='')
			{
				$mytype.=$type;
			}

			$sql ="SELECT count(*) as result FROM reservations ".$mytype;
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('reservations');
				$this->db->limit($num, $offset);
				//$this->db->order_by("register_date", "desc");
				
				  if ($_type==0)
				  {
					  $this->db->where("((email LIKE '%".$_keyword."%' ) OR (name LIKE '%".$_keyword."%' ) OR (date LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (info LIKE '%".$_keyword."%' ))");
				  }
				  if ($_type==1)
				  {
					  $clause="(status=0) and ((email LIKE '%".$_keyword."%' ) OR (name LIKE '%".$_keyword."%' ) OR (date LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (info LIKE '%".$_keyword."%' ))";
						$this->db->where($clause);
					  
				  }
				  if ($_type==2)
				  {
						
						$clause="(status=1) and ((email LIKE '%".$_keyword."%' ) OR (name LIKE '%".$_keyword."%' ) OR (date LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (info LIKE '%".$_keyword."%' ))";
						$this->db->where($clause);
				  }
				  if ($_type==3)
				  {
					$clause="(status=2) and ((email LIKE '%".$_keyword."%' ) OR (name LIKE '%".$_keyword."%' ) OR (date LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (info LIKE '%".$_keyword."%' ))";
						$this->db->where($clause);
					  
				  }
			//$this->db->where("(video!='NULL' or image!='NULL')");
			$this->db->order_by('id','desc');
			$query = $this->db->get();
			return $query;
		}
	}
	
	function delete_reserve($id)
	{
	// begin TRANSACTION
		$this->db->trans_start(); 
		
		$this->db->delete('reservations', array('id' => $id)); 
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
	function update_status_reserve($arr)
	{
		$this->db->trans_start();
			$this->db->set('status',$arr['status_option']);
			$this->db->where('id',$arr['update_id']);
			$this->db->update('reservations'); 
		$this->db->trans_complete();
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
	

// ==============  BOOKING MANAGER ======================	

	function get_booking($totalrow,$_type='',$_keyword='',$num, $offset)
	{
		if ($totalrow=="totalrow")
		{
			
			$mytype='';
			$type='';
			if ($_type==0)
			{
				$type=" where ((email LIKE '%".$_keyword."%' ) OR (contact_name LIKE '%".$_keyword."%' )  OR (date1 LIKE '%".$_keyword."%' ) OR (date2 LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (special LIKE '%".$_keyword."%' ))";
			}
			if ($_type==1)
			{
				$type=" and ((email LIKE '%".$_keyword."%' ) OR (contact_name LIKE '%".$_keyword."%' )  OR (date1 LIKE '%".$_keyword."%' ) OR (date2 LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (special LIKE '%".$_keyword."%' ))";
				$mytype=" where status=0";
			}
			if ($_type==2)
			{
				$type=" and ((email LIKE '%".$_keyword."%' ) OR (contact_name LIKE '%".$_keyword."%' )  OR (date1 LIKE '%".$_keyword."%' ) OR (date2 LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (special LIKE '%".$_keyword."%' ))";
				$mytype=" where status=1";
			}
			if ($_type==3)
			{
				$type=" and ((email LIKE '%".$_keyword."%' ) OR (contact_name LIKE '%".$_keyword."%' ) OR (date1 LIKE '%".$_keyword."%' ) OR (date2 LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (special LIKE '%".$_keyword."%' ))";
				$mytype=" where status=2";
			}
			if($_keyword!='')
			{
				$mytype.=$type;
			}

			$sql ="SELECT count(*) as result FROM booking ".$mytype;
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('booking');
				$this->db->limit($num, $offset);
				//$this->db->order_by("register_date", "desc");
				
				  if ($_type==0)
				  {
					  $this->db->where("((email LIKE '%".$_keyword."%' ) OR (contact_name LIKE '%".$_keyword."%' ) OR (date1 LIKE '%".$_keyword."%' ) OR (date2 LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (special LIKE '%".$_keyword."%' ))");
				  }
				  if ($_type==1)
				  {
					  $clause="(status=0) and ((email LIKE '%".$_keyword."%' ) OR (contact_name LIKE '%".$_keyword."%' ) OR (date1 LIKE '%".$_keyword."%' ) OR (date2 LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (special LIKE '%".$_keyword."%' ))";
						$this->db->where($clause);
					  
				  }
				  if ($_type==2)
				  {
						
						$clause="(status=1) and ((email LIKE '%".$_keyword."%' ) OR (contact_name LIKE '%".$_keyword."%' ) OR (date1 LIKE '%".$_keyword."%' ) OR (date2 LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (special LIKE '%".$_keyword."%' ))";
						$this->db->where($clause);
				  }
				  if ($_type==3)
				  {
					$clause="(status=2) and ((email LIKE '%".$_keyword."%' ) OR (contact_name LIKE '%".$_keyword."%' ) OR (date1 LIKE '%".$_keyword."%' ) OR (date2 LIKE '%".$_keyword."%' ) OR (phone LIKE '%".$_keyword."%' ) OR (special LIKE '%".$_keyword."%' ))";
						$this->db->where($clause);
					  
				  }
			//$this->db->where("(video!='NULL' or image!='NULL')");
			$this->db->order_by('id','desc');
			$query = $this->db->get();
			return $query;
		}
	}
	
	function delete_booking($id)
	{
	// begin TRANSACTION
		$this->db->trans_start(); 
		
		$this->db->delete('booking', array('id' => $id)); 
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
	function update_status_booking($arr)
	{
		$this->db->trans_start();
			$this->db->set('status',$arr['status_option']);
			$this->db->where('id',$arr['update_id']);
			$this->db->update('booking'); 
		$this->db->trans_complete();
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}

// =================== USER MANAGER ====================

	function get_user_manager($totalrow,$_type='',$_keyword='',$num, $offset)
	{
		if ($totalrow=="totalrow")
		{
			
			$mytype='';
			$type='';
			if ($_type==0)
			{
				$type=" where ((email LIKE '%".$_keyword."%' ) OR (username LIKE '%".$_keyword."%' )  OR (birthdate LIKE '%".$_keyword."%' )  OR (phone LIKE '%".$_keyword."%' ) OR (address LIKE '%".$_keyword."%' ))";
			}
			if ($_type==1)
			{
				$type=" and ((email LIKE '%".$_keyword."%' ) OR (username LIKE '%".$_keyword."%' )  OR (birthdate LIKE '%".$_keyword."%' )  OR (phone LIKE '%".$_keyword."%' ) OR (address LIKE '%".$_keyword."%' ))";
				$mytype=" where active=0";
			}
			if ($_type==2)
			{
				$type=" and ((email LIKE '%".$_keyword."%' ) OR (username LIKE '%".$_keyword."%' )  OR (birthdate LIKE '%".$_keyword."%' )  OR (phone LIKE '%".$_keyword."%' ) OR (address LIKE '%".$_keyword."%' ))";
				$mytype=" where active=1";
			}
			if($_keyword!='')
			{
				$mytype.=$type;
			}

			$sql ="SELECT count(*) as result FROM ua_users ".$mytype;
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('ua_users');
				$this->db->limit($num, $offset);
				//$this->db->order_by("register_date", "desc");
				
				  if ($_type==0)
				  {
					  $this->db->where("((email LIKE '%".$_keyword."%' ) OR (username LIKE '%".$_keyword."%' ) OR (birthdate LIKE '%".$_keyword."%' )  OR (phone LIKE '%".$_keyword."%' ) OR (address LIKE '%".$_keyword."%' ))");
				  }
				  if ($_type==1)
				  {
					  $clause="(active=0) and ((email LIKE '%".$_keyword."%' ) OR (username LIKE '%".$_keyword."%' ) OR (birthdate LIKE '%".$_keyword."%' )  OR (phone LIKE '%".$_keyword."%' ) OR (address LIKE '%".$_keyword."%' ))";
						$this->db->where($clause);
					  
				  }
				  if ($_type==2)
				  {
						
						$clause="(active=1) and ((email LIKE '%".$_keyword."%' ) OR (username LIKE '%".$_keyword."%' ) OR (birthdate LIKE '%".$_keyword."%' )  OR (phone LIKE '%".$_keyword."%' ) OR (address LIKE '%".$_keyword."%' ))";
						$this->db->where($clause);
				  }

			//$this->db->where("(video!='NULL' or image!='NULL')");
			$this->db->order_by('ua_user_id','desc');
			$query = $this->db->get();
			return $query;
		}
	}
	
	function delete_user($id)
	{
	// begin TRANSACTION
		$this->db->trans_start(); 
		
		$this->db->delete('ua_users', array('ua_user_id' => $id)); 
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}
//========= EVENTS MANAGER ========================
	function get_mailtemplate($totalrow='',$num='', $offset='')
	{
		if ($totalrow=="totalrows")
		{
			
			$sql ="SELECT count(*) as result from newsletters";
			$query = $this->db->query($sql);
			foreach ($query->result() as $row)
				return $row->result;
		}
		else
		{
				$this->db->select("*");
				$this->db->from('newsletters');
				$this->db->limit($num, $offset);
				$this->db->order_by('date_created','desc');
			$query = $this->db->get();
			return $query;
		}
	}
	function get_mailtemplate_detail($id)
	{
		$this->db->select('*');
		$this->db->from('newsletters');
		$this->db->where('id', $id);
		$query = $this->db->get();		
		return $query;
	}
	function mailtemplate_manipulate($data)
	{
		$this->db->trans_start(); 
		$config['upload_path'] = './upload_file/article/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png';
		$config['max_size']	= '6000';
		$this->load->library('upload', $config);
		if($data['currentid']!='') // update
		{
				$id=$data['currentid']; 
				if($_FILES['image']['name'])
				{
					
						if ( $this->upload->do_upload('image'))
						{	
								$datax1=$this->upload->data();
								$this->db->set('image',$datax1['file_name']);
								
						}
						else
						{
							return -1;
						}
				}
		
				
				$this->db->where('id',$id);
				$this->db->set('title',$data['txtName1']);
				$this->db->set('body',$data['maincontent1']);
				$this->db->update('newsletters'); 
		
		}
		else	//add
		{
					if($_FILES['image']['name'])
					{
						
							if ( $this->upload->do_upload('image'))
							{	
									$datax1=$this->upload->data();
							}
							else
							{
								//echo $this->upload->display_errors('<p>', '</p>');
								return -1;
							}
					}
					else
					{
						$datax1['file_name']=NULL;				
					}
				
				$this->db->set('date_created',date('Y-m-d',time()));
				$this->db->set('title',$data['txtName1']);
				$this->db->set('image',$datax1['file_name']);
				$this->db->set('body',$data['maincontent1']);
				$this->db->insert('newsletters'); 
				

		}
		$this->db->trans_complete();
		// end TRANSACTION			
		if ($this->db->trans_status() == FALSE)
		{
			return 0;
		} 
		else
		{
			return 1;
		}
	}


}
